{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import joblib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(33465, 5460)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = joblib.load('../../scrum_data/train_data/train_null_sign.lz4')\n",
    "data_label = pd.read_csv('../../preprocess_data/train_y_33465.csv',usecols=['label'])\n",
    "x = data.fillna(-1).values\n",
    "y = data_label.values\n",
    "x.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 初始化xgb model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'num_boost_round' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-11-357e11ebba1d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     19\u001b[0m     }\n\u001b[1;32m     20\u001b[0m \u001b[0mwatchlist\u001b[0m  \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdtrain\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'train'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 21\u001b[0;31m \u001b[0mmodel_33465\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mxgb\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mparams\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mdtrain\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mnum_boost_round\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnum_boost_round\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mevals\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mwatchlist\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mverbose_eval\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'num_boost_round' is not defined"
     ]
    }
   ],
   "source": [
    "import xgboost as xgb\n",
    "dtrain = xgb.DMatrix(x,y,feature_names=data.columns)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0]\ttrain-auc:0.652595\n",
      "[10]\ttrain-auc:0.695954\n",
      "[20]\ttrain-auc:0.714873\n",
      "[30]\ttrain-auc:0.730022\n",
      "[40]\ttrain-auc:0.740691\n",
      "[50]\ttrain-auc:0.746111\n",
      "[60]\ttrain-auc:0.753499\n",
      "[70]\ttrain-auc:0.759688\n",
      "[80]\ttrain-auc:0.764973\n",
      "[90]\ttrain-auc:0.769682\n",
      "[100]\ttrain-auc:0.773679\n",
      "[110]\ttrain-auc:0.77747\n",
      "[120]\ttrain-auc:0.78166\n",
      "[130]\ttrain-auc:0.785796\n",
      "[140]\ttrain-auc:0.789334\n",
      "[149]\ttrain-auc:0.792686\n"
     ]
    }
   ],
   "source": [
    "params={\n",
    "'booster':'gbtree',\n",
    "'objective': 'binary:logistic',\n",
    "'early_stopping_rounds':100,\n",
    "'scale_pos_weight': float(len(y)-np.sum(y))/float(np.sum(y)),  # 负例样本除以正例样本\n",
    "'eval_metric': 'auc',\n",
    "'gamma':0.1,\n",
    "'max_depth':4,\n",
    "'lambda':10,\n",
    "'subsample':0.9,\n",
    "'colsample_bytree':0.9,\n",
    "'eta': 0.04,\n",
    "'seed':2018,\n",
    "'nthread':18\n",
    "    }\n",
    "watchlist  = [(dtrain,'train')]\n",
    "model_33465 = xgb.train(params,dtrain,num_boost_round=150,evals=watchlist, verbose_eval=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['model_33465']"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "joblib.dump(model_33465,'model_33465')\n",
    "joblib.dump(list(data.columns),'feature_names')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### tag=0的部分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0]\ttrain-auc:0.643012\n",
      "[10]\ttrain-auc:0.683014\n",
      "[20]\ttrain-auc:0.699693\n",
      "[30]\ttrain-auc:0.709707\n",
      "[40]\ttrain-auc:0.718901\n",
      "[50]\ttrain-auc:0.725988\n",
      "[60]\ttrain-auc:0.732488\n",
      "[70]\ttrain-auc:0.738239\n",
      "[80]\ttrain-auc:0.742093\n",
      "[90]\ttrain-auc:0.746669\n",
      "[100]\ttrain-auc:0.749507\n",
      "[110]\ttrain-auc:0.753453\n",
      "[120]\ttrain-auc:0.756372\n",
      "[130]\ttrain-auc:0.759052\n",
      "[140]\ttrain-auc:0.761537\n",
      "[149]\ttrain-auc:0.764038\n"
     ]
    }
   ],
   "source": [
    "import xgboost as xgb\n",
    "dtrain = xgb.DMatrix(x[:-3000],y[:-3000],feature_names=data.columns)\n",
    "params={\n",
    "'booster':'gbtree',\n",
    "'objective': 'binary:logistic',\n",
    "'early_stopping_rounds':100,\n",
    "'scale_pos_weight': float(len(y)-np.sum(y))/float(np.sum(y)),  # 负例样本除以正例样本\n",
    "'eval_metric': 'auc',\n",
    "'gamma':0.1,\n",
    "'max_depth':3,\n",
    "'lambda':10,\n",
    "'subsample':0.9,\n",
    "'colsample_bytree':0.9,\n",
    "'eta': 0.04,\n",
    "'seed':2018,\n",
    "'nthread':18\n",
    "    }\n",
    "watchlist  = [(dtrain,'train')]\n",
    "model_30465 = xgb.train(params,dtrain,num_boost_round=150,evals=watchlist, verbose_eval=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['model_30465']"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "joblib.dump(model_30465,'model_30465')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## tag=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0]\ttrain-auc:0.619081\n",
      "[10]\ttrain-auc:0.693724\n",
      "[20]\ttrain-auc:0.707035\n",
      "[30]\ttrain-auc:0.714302\n",
      "[40]\ttrain-auc:0.725864\n",
      "[50]\ttrain-auc:0.734763\n",
      "[60]\ttrain-auc:0.740762\n",
      "[70]\ttrain-auc:0.744774\n",
      "[80]\ttrain-auc:0.75014\n",
      "[90]\ttrain-auc:0.757322\n",
      "[100]\ttrain-auc:0.764599\n",
      "[110]\ttrain-auc:0.770274\n",
      "[120]\ttrain-auc:0.776341\n",
      "[130]\ttrain-auc:0.780004\n",
      "[140]\ttrain-auc:0.784639\n",
      "[150]\ttrain-auc:0.788107\n",
      "[160]\ttrain-auc:0.791566\n",
      "[170]\ttrain-auc:0.795649\n",
      "[179]\ttrain-auc:0.800997\n"
     ]
    }
   ],
   "source": [
    "import xgboost as xgb\n",
    "dtrain = xgb.DMatrix(x[-3000:],y[-3000:],feature_names=data.columns)\n",
    "params={\n",
    "'booster':'gbtree',\n",
    "'objective': 'binary:logistic',\n",
    "'early_stopping_rounds':100,\n",
    "'scale_pos_weight': float(len(y)-np.sum(y))/float(np.sum(y)),  # 负例样本除以正例样本\n",
    "'eval_metric': 'auc',\n",
    "'gamma':0.1,\n",
    "'max_depth':2,\n",
    "'lambda':10,\n",
    "'subsample':0.9,\n",
    "'colsample_bytree':0.9,\n",
    "'eta': 0.04,\n",
    "'seed':2018,\n",
    "'nthread':18\n",
    "    }\n",
    "watchlist  = [(dtrain,'train')]\n",
    "model_3000 = xgb.train(params,dtrain,num_boost_round=180,evals=watchlist, verbose_eval=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['model_3000']"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "joblib.dump(model_3000,'model_3000')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
